package com.viewplug.view.scroll;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Scroller;

import com.viewplug.view.swap.SwapListView;

/**
 * Created by mac_wk on 14-12-22.
 */
public class WScrollView extends LinearLayout implements View.OnTouchListener {
    private WSrollViewHeader mHeaderView;
    private final static int SCROLL_DURATION = 400; // scroll back duration
    private PointF mLastMove = new PointF();
    private float mResistance = 1.7f;
    private ListView mContentListView;
    public final static  int DEFAULT = 0;
    public final static  int SCROLLDOWN_HEADER = 1;
    public final static  int SCROLLBACK_HEADER = 2;
    public final static  int REFRESHING = 3;

    private int status=0;//状态 0,代表默认状态，1代表下拉状态,2代表上拉状态，3代表刷新状态
    public WScrollView(Context context) {
        this(context, null);
    }

    public WScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mHeaderView = new WSrollViewHeader(context);
        setOrientation(VERTICAL);

        addView(mHeaderView, 0);

    }
    public WScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public WScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        this(context, attrs, defStyleAttr);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
       // return super.onInterceptTouchEvent(ev);
    }
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {


        if (!isEnabled() || mContentListView == null || mHeaderView == null) {
            return super.dispatchTouchEvent(ev);
        }
        int action = ev.getAction();
        switch (action)
        {
            case MotionEvent.ACTION_DOWN:
                mLastMove.set(ev.getX(),ev.getY());
                return super.dispatchTouchEvent(ev);
            case MotionEvent.ACTION_MOVE:
                if (mHeaderView.getState()==REFRESHING)
                    return super.dispatchTouchEvent(ev);
                float offsetX = ev.getX()-mLastMove.x;
                float offsetY = ev.getY() - mLastMove.y;
                mLastMove.set(ev.getX(), ev.getY());
                if(mContentListView.getFirstVisiblePosition() == 0)
                {

                    if (offsetY>0)
                    {
                        move((int) (offsetY/mResistance));
                        return false;
                    }else
                    {
                        if(mHeaderView.getVisibileHeight()<=0)
                            return super.dispatchTouchEvent(ev);
                        move((int) (offsetY/mResistance));
                        return false;
                    }
                }else
                {
                    return super.dispatchTouchEvent(ev);
                }
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (mHeaderView.getState()==REFRESHING)
                    return super.dispatchTouchEvent(ev);
                resetHeader();
                total = 0;
                return super.dispatchTouchEvent(ev);
        }
        return super.dispatchTouchEvent(ev);
    }
    private void resetHeader() {
        int height = mHeaderView.getVisibileHeight();
        if (height<=0)
            return;

        if (height>mHeaderView.getHeader_height())
        {
            mHeaderView.resetHeader(mHeaderView.getMeasuredHeight());
            if (refreshListener!=null&&mHeaderView.getState()!=REFRESHING)
            {
                mHeaderView.setState(REFRESHING);
                refreshListener.onRefresh();
            }

        }else
        {
            mHeaderView.resetHeader(mHeaderView.getMeasuredHeight()+mHeaderView.getHeader_height());
        }
    }
    public void stopRefresh()
    {
        mHeaderView.setState(DEFAULT);
        mHeaderView.resetHeader(mHeaderView.getMeasuredHeight()+mHeaderView.getHeader_height());

    }

    private int total=0;
    private void move(int deltaY)
    {
        if (deltaY==0)
            return;
        total+=deltaY;
        mHeaderView.scrollY(deltaY);
        invalidate();
        if (mHeaderView.getState()==REFRESHING)
            return;
        if (total>mHeaderView.getHeader_height())
        {
            int h_state = mHeaderView.getState();
            if (h_state!=SCROLLDOWN_HEADER)//判断如果不是下拉状态 就设置下拉图标
            {
                mHeaderView.setState(SCROLLDOWN_HEADER);
            }
        }else
        {
            int h_state = mHeaderView.getState();
            if (h_state!=SCROLLBACK_HEADER)//判断如果不是下拉状态 就设置下拉图标
            {
                mHeaderView.setState(SCROLLBACK_HEADER);
            }
        }
    }
    private RefreshListener refreshListener;


    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    public ListView getmContentListView() {
        return mContentListView;
    }

    public static interface RefreshListener{
        public void onRefresh();
        public void onLoadMore();
        public void onFirst();
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
    @Override
    protected void onFinishInflate() {
        mContentListView = (ListView) getChildAt(1);
        super.onFinishInflate();
    }
}
